<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 */
 require_once ROOT_PATH . '/lib/common/SearchObject.php';
require_once ROOT_PATH.'/lib/models/skillmatrix/SkillGroup.php';
require_once ROOT_PATH.'/lib/models/skillmatrix/SkillExpertise.php';
require_once ROOT_PATH.'/lib/models/skillmatrix/SkillUser.php';

 class EXTRACTOR_SkillMatrix {

	
	
	public function parseAddSkillGroup($postArr){
		$skillGroup = new SkillGroup();
		$skillGroup->setSkillGroupName($postArr['skill_group_name']);
		
 		if (isset($postArr['skill_group_type']) && !empty($postArr['skill_group_type'])) {
			$skillGroup->setSkillGroupType(implode(",",$postArr['skill_group_type']));
		}		
		return $skillGroup;
	}	
 	public function parseUpdateSkillGroup($postArr){
		$id=$postArr['skill_group_id'];
 		$skillGroup = SkillGroup::getSkillGroup($id);
		
		$skillGroup->setSkillGroupName($postArr['skill_group_name']);
		
 		if (isset($postArr['skill_group_type'])) {
			$skillGroup->setSkillGroupType(implode(",",$postArr['skill_group_type']));
		}		
		return $skillGroup;
	}
	
 	public function parseAddSkillExpertise($postArr){
		$skillExpertise = new SkillExpertise();
		$skillExpertise->setSkillExpertiseName($postArr['skill_expertise_name']);
		$skillExpertise->setSkillGroupID($postArr['skill_expertise_group_id']);
		$skillExpertise->setSkillExpertiseOrder(0);
		return $skillExpertise;
	}
	
	public function parseAddSkillUsers($postArr,$userID){
		$skillGroups = SkillGroup::getAll();
		
		//	$postArr['txtFromDate'] = LocaleUtil::getInstance()->convertToStandardDateFormat($postArr['txtFromDate']);
		$date = date('Y-m-d');
		$skillIndividual = SkillIndividual::getSkillIndividualByUser($userID);				
		if(!$skillIndividual->getId())	
			$skillIndividual = new SkillIndividual(); 	
		$skillIndividual->setSkillIndividualUserID($userID);
		if(isset($_POST['numofyear'])){
			$skillIndividual->setSkillIndividualNumOfYear($_POST['numofyear']);
		}
		if(isset($_POST['work_abroad'])){
			$skillIndividual->setSkillIndividualWorkAbroad($_POST['work_abroad']);
		}
		
		if(isset($_POST['expertise'])){
			$skillIndividual->setSkillIndividualExpertise($_POST['expertise']);
		}
		$skillIndividual->setSkillIndividualDateSubmit($date);
		
		$skillUsers = array();
		foreach($skillGroups as $skillGroups){
			$skillExpertises = SkillGroup::getSkillExpertise($skillGroups->getId());
			if(count($skillExpertises)>0){
				foreach($skillExpertises as $skillExpertise){
					$skillExpertiseID = $skillExpertise->getId();
					if(isset($postArr["skill$skillExpertiseID"]) ){
						$existUserSkill = SkillExpertise::getUserSkills($skillExpertiseID,$userID);
						if($existUserSkill){							
							if($existUserSkill[0]->getSkillUserValue()!=$postArr["skill$skillExpertiseID"]){
								$existUserSkill[0]->setSkillUserValue($postArr["skill$skillExpertiseID"]);
								$existUserSkill[0]->setSkillUserDateSubmit($date);
								$skillUsers[]= $existUserSkill[0];
							}	
						}else{
						$skillUser = new SkillUser();
						$skillUser->setSkillUserExpertiseID($skillExpertiseID);
						$skillUser->setSkillUserUserID($userID);
						$skillUser->setSkillUserValue($postArr["skill$skillExpertiseID"]);
						$skillUser->setSkillUserDateSubmit($date);
						$skillUsers[]= $skillUser;	
						}
						
					}
				}
			}
		}
		
		$array= array();
		$array['skillUsers'] = $skillUsers;
		$array['skillindividual'] = $skillIndividual;
		return $array;
		
	}

}
?>